<?php
if(!defined('BLOG'))exit('不作死就不会死');
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/1/9
 * Time: 16:06
 */
class memcache_d{
    private $timeout,$key,$m,$prefix;

    /**
     * @param $config
     * @param $prefix
     */
    public function __construct($config,$prefix){
        $this->timeout = $config['timeout'];
        $this->prefix = $prefix;
        $this->connect($config['servers']);
    }

    /**
     * @param $servers
     */
    public function connect($servers){
            $this->m = new Memcached();
            $this->m->addServers($servers);
           /* foreach($servers as $server){
                $this->m->addServer($server[0],$server[1],$persistent=true,$server[2]);
            }*/

    }

    /**
     * @param $key
     * @param $val
     * @return mixed
     */
    public function add($key,$val){
        $this->generate_key($key);
        return $this->m->set($this->key,$val);
    }

    /**
     * @param $key
     * @return mixed
     */
    public function get($key){
        $this->generate_key($key);
        return $this->m->get($this->key);
    }

    /**
     * @param $key
     * @return mixed
     */
    public function delete($key){
        $this->generate_key($key);
        return $this->m->delete($this->key);
    }

    /**
     * @return mixed
     */
    public function flush(){
        return $this->m->flush();
    }

    /**
     * @param $key
     * @param $val
     * @return mixed
     */
    public function replace($key,$val){
        $this->generate_key($key);
        return $this->m->replace($this->key,$val);
    }

    /**
     * @param $key
     * @param $val
     * @return mixed
     */
    public function set($key,$val){
        $this->generate_key($key);
        return $this->m->set($this->key,$val);

    }

    /**
     * @param $key
     */
    public function generate_key($key){
        $this->key =  $this->prefix.md5($key);
    }

    /**
     * @param $key
     * @return mixed
     */
    public  function is_cached($key){
        return $this->get($key);
    }
}